package com.wjf.accesscontrol.ui.setting

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.wjf.accesscontrol.base.MVVMBaseFragment
import com.wjf.accesscontrol.databinding.FragmentLoginBinding
import com.wjf.accesscontrol.entity.LoginEntity
import com.wjf.accesscontrol.net.utils.Status
import com.wjf.accesscontrol.ui.TitleBarActivity
import com.wjf.accesscontrol.ui.read.dialog.DIALOG_TYPE_3
import com.wjf.accesscontrol.util.AppUtils
import com.wjf.accesscontrol.util.Constant
import com.wjf.accesscontrol.util.JUMP_TO_SettingFragment
import com.wjf.accesscontrol.util.LogUtils
import com.wjf.accesscontrol.util.SPUtils
import com.wjf.accesscontrol.util.SP_KEY_BASE_URL
import com.wjf.accesscontrol.util.SP_KEY_MANAGER_URL
import com.wjf.accesscontrol.util.ToastUtils
import com.wjf.accesscontrol.util.singleClick

/**
 * @Description
 * @Author WuJianFeng
 * @Date 2024/1/30 17:24
 *
 */

class LoginFragment : MVVMBaseFragment<SettingViewModel, FragmentLoginBinding>() {

    override fun initViewModel() = ViewModelProviders.of(this).get(SettingViewModel::class.java)


    override fun initViewBinding(inflater: LayoutInflater, container: ViewGroup?)=
        FragmentLoginBinding.inflate(inflater,container,false)

    override fun initData() {
        binding.etAdd.setText(Constant.managerUrl)
    }
    override fun initClick() {
        binding.tvReturn.singleClick {
            activity?.finish()
        }
        binding.tvBtn.singleClick {

            if (binding.etPhone.text.isEmpty()){
                ToastUtils.show("手机号不能为空")
                return@singleClick
            }

            if (binding.etPassword.text.isEmpty()){
                ToastUtils.show("密码不能为空")
                return@singleClick
            }

            if (binding.etAdd.text.isEmpty()){
                ToastUtils.show("地址不能为空")
                return@singleClick
            }
            vm.login(binding.etAdd.text.toString(),binding.etPhone.text.toString(),binding.etPassword.text.toString()).observe(this, Observer {
                it?.let { resource ->
                    when (resource.status) {
                        Status.SUCCESS -> {
                            LogUtils.d("__getUsers-SUCCESS","1")
                            (resource.data as LoginEntity).apply {
                                SPUtils.instance.commit(SP_KEY_MANAGER_URL,binding.etAdd.text.toString())
                                Constant.managerUrl =  binding.etAdd.text.toString()

                                Constant.baseUrl = "${this.businessUrl}"
                                SPUtils.instance.commit(SP_KEY_BASE_URL,"${this.businessUrl}")

                                TitleBarActivity.newInstance(mView.context,JUMP_TO_SettingFragment, data = "${this.businessUrl}")
                            }
                        }
                        Status.ERROR -> {
                            LogUtils.d("__getUsers-ERROR","${it.message}")
                            AppUtils.showResultDialog(
                                fragmentManager = childFragmentManager,
                                resultDialogCallback = null,
                                type = DIALOG_TYPE_3,
                                msg = "${it.message}"
                            )

                        }
                        Status.LOADING -> {
                            LogUtils.d("__getUsers-LOADING","1")
                        }
                    }
                }
            })

        }
    }

}